'Standard Initial Review Requirement Script  
Option Strict Off
imports System
imports System.Collections
imports PHSRAG.Insight21.Humans
imports PHSRAG.Insight21.BusinessObjects
imports FormsManagement = PHSRAG.Insight21.FormsManagement

module Script
sub Main()

    Dim species as string = questAnswer.GetAnswerValue("Species")
    Dim noOfAnimals as string = questAnswer.GetAnswerValue("TotNumAnimals")

    ' Determine if questionnaire answers for multiple types are already added
    Dim hazardExists as Boolean
    Dim tumorExists as Boolean
    Dim procExists as Boolean
    Dim anesExists as Boolean
    for each qa as FormsManagement.QuestionnaireAnswer in currentProtVersion.QuestionnaireAnswers
        select case qa.QuestionnaireCategory
            case "QUESTCAT_ANM_TP"
                tumorExists = true
            case "QUESTCAT_ANM_PRO"
                procExists = true
            case "QUESTCAT_ANM_HZ"
                hazardExists = true
            case "QUESTCAT_ANM_AN"
                anesExists = true
        end select
    next

    theProtocol.speciesCode = species
    Dim qv as FormsManagement.QuestionnaireVersion

    if species = "DOGS" then
        qv = FormsManagement.Questionnaire.GetActiveVersion( cachePolicy, "Dog Exercise", FormsManagement.Constants.QuestionnaireType.Animals )
        currentProtVersion.AddNewQuestionnaireAnswer(qv.Id, cachePolicy, "Dog Exercise", not qv.Questionnaire.AllowMultiple)
        currentProtVersion.DeleteQuestionnaireAnswer("NHP Psychological Enrichment")        
    else if LEFT(species, 7) = "MONKEYS" then
        qv = FormsManagement.Questionnaire.GetActiveVersion( cachePolicy, "NHP Psychological Enrichment", FormsManagement.Constants.QuestionnaireType.Animals )
        currentProtVersion.AddNewQuestionnaireAnswer(qv.Id, cachePolicy, "NHP Psychological Enrichment", not qv.Questionnaire.AllowMultiple)
        currentProtVersion.DeleteQuestionnaireAnswer("Dog Exercise")
    else
        currentProtVersion.DeleteQuestionnaireAnswer("Dog Exercise")    
        currentProtVersion.DeleteQuestionnaireAnswer("NHP Psychological Enrichment")    
    end if
       
    theProtocol.NumberOfAnimals = noOfAnimals
    
    Dim answerNameValuePairs as new ArrayList
    answerNameValuePairs.Add( "LD50" )
    answerNameValuePairs.Add( "LD50" )
    answerNameValuePairs.Add( "Moribund" )
    answerNameValuePairs.Add( "MORIBUND" )
    AddForm( answerNameValuePairs, "Morbidity and Mortality", false )
    
    answerNameValuePairs.Clear
    answerNameValuePairs.Add( "Tran" )
    answerNameValuePairs.Add( "TRAN" )
    answerNameValuePairs.Add( "IntraTran" )
    answerNameValuePairs.Add( "INTRATRAN" )
    AddForm( answerNameValuePairs, "Transportation", false )
    
    answerNameValuePairs.Clear
    answerNameValuePairs.Add( "PainStudy" )
    answerNameValuePairs.Add( "PAIN" )
    answerNameValuePairs.Add( "UnPain" )
    answerNameValuePairs.Add( "UNPAIN" )
    AddForm( answerNameValuePairs, "Pain and/or Distress Study Justification", false )
    
    Dim valueMatches as boolean
    AddForm( "SxProcedures", "MxSx", "Multiple Survival procedures", false, valueMatches )
    AddForm( "Paralytic", "PARALYTICS", "Paralytics", false, valueMatches )
    AddForm( "Breed", "BREED", "Breeding Studies", false, valueMatches )
    AddForm( "Restraint", "REST", "Restraint", false, valueMatches )
    AddForm( "Fluid", "FLUID", "Controlled Food or Fluid Intake", false, valueMatches )
    AddForm( "AnesAgent", "ANES", "Anesthesia Regimen", anesExists, valueMatches )
    AddForm( "Ascites", "ASCITES", "Antibody & Ascites Production", false, valueMatches )
    AddForm( "Hazard", "Yes", "Hazardous Agent Administration and Use", hazardExists, valueMatches )
    AddForm( "Tumor", "TUMOR", "Tumor Production", tumorExists, valueMatches )
    theProtocol.TissueTest = valueMatches

    answerNameValuePairs.Clear
    answerNameValuePairs.Add( "Surgery" )
    answerNameValuePairs.Add( "Sx" )
    answerNameValuePairs.Add( "SxProcedures" )
    answerNameValuePairs.Add( "MxSx" )
    answerNameValuePairs.Add( "NonSx" )
    answerNameValuePairs.Add( "Proc" )
    AddForm( answerNameValuePairs, "Procedures", procExists )
    
    theProtocol.Save( connectionString )
    currentProtVersion.Save ( connectionString )
    currentProcess.Save( connectionString )
end sub  

' Adds or removes questionnaire answers to protocol version based on answer to a question
sub AddForm(byval answerName as string, byval answerValue as string, byval questionnaireName as string, byval formExists as boolean, byref valueMatches as boolean)
    Dim answers as Arraylist = questAnswer.GetAnswers( answerName )
    if answers.Count > 0 then
        if answers(0).Value = answerValue and not formExists then
            valueMatches = true
            Dim qv as FormsManagement.QuestionnaireVersion = FormsManagement.Questionnaire.GetActiveVersion( cachePolicy, questionnaireName, FormsManagement.Constants.QuestionnaireType.Animals )
            Dim qa as FormsManagement.QuestionnaireAnswer = currentProtVersion.AddNewQuestionnaireAnswer(qv.Id, cachePolicy, questionnaireName, not qv.Questionnaire.AllowMultiple)
        else if answers(0).Value = answerValue
            valueMatches = true
        else if answers(0).Value <> answerValue then
            valueMatches = false
            currentProtVersion.DeleteQuestionnaireAnswer(questionnaireName)
        end if 
    else
        valueMatches = false
        currentProtVersion.DeleteQuestionnaireAnswer(questionnaireName)
    end if
end sub

' Adds or removes questionnaire answers to protocol version based on answer to a question
' This overload takes mutliple questions / answers to determine if the QA needs to be added or 
' removed.
sub AddForm(byval answerNameValuePairs as ArrayList, byval questionnaireName as string, byval formExists as boolean)
    Dim add as boolean = false
    Dim i as integer
    for i = 0 to answerNameValuePairs.Count - 1 step 2
        Dim answers as Arraylist = questAnswer.GetAnswers( answerNameValuePairs(i) )
        if answers.Count > 0 then
            if answers(0).Value = answerNameValuePairs(i+1) then
                add = true
            end if
        end if
    next
    if add and not formExists then
        Dim qv as FormsManagement.QuestionnaireVersion = FormsManagement.Questionnaire.GetActiveVersion( cachePolicy, questionnaireName, FormsManagement.Constants.QuestionnaireType.Animals )
        Dim qa as FormsManagement.QuestionnaireAnswer = currentProtVersion.AddNewQuestionnaireAnswer(qv.Id, cachePolicy, questionnaireName, not qv.Questionnaire.AllowMultiple)
    else if not add then
        currentProtVersion.DeleteQuestionnaireAnswer(questionnaireName)
    end if
end sub

end module
